//
//  YanStatusPictureView.swift
//  SinaWeibo
//
//  Created by mac on 2017/5/27.
//  Copyright © 2017年 mac. All rights reserved.
//

import UIKit

class YanStatusPictureView: UIView {

    var viewModel: YanStatusViewModel? {
        didSet {
            calcViewSize()
            
            urls = viewModel?.picURLs
        }
    }
    
    //根据视图模型的配图视图大小，调整显示内容
    fileprivate func calcViewSize() {
        
        // 处理高度
        // 1.单图 根据配图视图的大小，修改 subview[0] 的宽高
        if viewModel?.picURLs?.count == 1 {
            // a.获取第0个图像视图
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            
            
            let v = subviews[0]
            v.frame = CGRect(x: 0,
                             y: YanStatusPictureViewOutterMargin,
                             width: viewSize.width,
                             height: viewSize.height - YanStatusPictureViewOutterMargin)
            
        } else {
            // 2.多图 回复 subview[0] 的宽高，保证九宫格布局的完整
            
            let v = subviews[0]
            v.frame = CGRect(x: 0,
                             y: YanStatusPictureViewOutterMargin,
                             width: YanStatusPictureItemWidth,
                             height: YanStatusPictureItemWidth)
            
        }
        
        
        
        
        // 修改高度约束
        heightCons.constant = viewModel?.pictureViewSize.height ?? 0
    }
    
    /// 配图试图的数组
    fileprivate var urls: [YanStatusPicture]? {
        didSet {
            // 1.隐藏所有的imageView
            for v in subviews {
                v.isHidden = true
            }
            
            // 2.遍历urls数组，顺序呢设置图像
            var index = 0
            
            for url in urls ?? [] {
                // 获得对应索引的imageview
                let iv = subviews[index] as! UIImageView
                
                // 4张图像处理
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                
                // 设置图像
                iv.cz_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                // 显示图像
                iv.isHidden = false
                
                index += 1
            }
        }
    }
    
    @IBOutlet weak var heightCons: NSLayoutConstraint!

    override func awakeFromNib() {
        setupUI()
    }
    
}

extension YanStatusPictureView {
    fileprivate func setupUI() {
        // 1.Cell中所有的空间都是提前准备好
        // 2.设置的时候，根据数据决定是否显示
        // 3.不能动态创建控件
        
        // 设置背景颜色
        backgroundColor = superview?.backgroundColor
        
        // 超出边界的内容不显示
        clipsToBounds = true
        
        let count = 9
        let rect = CGRect(x: 0,
                          y: YanStatusPictureViewOutterMargin,
                          width: YanStatusPictureItemWidth,
                          height: YanStatusPictureItemWidth)
        
        
        for i in 0..<count {
        
            let iv = UIImageView()
            
            // 设置contentMode
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            // 行 -> Y
            let row = CGFloat(i /  3)
            // 列 -> X
            let col = CGFloat(i % 3)

            let xOffset = col * (YanStatusPictureItemWidth + YanStatusPictureViewInnerMargin)
            let yOffset = row * (YanStatusPictureItemWidth + YanStatusPictureViewInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(iv)
            
        }
        
    }
}
